'use strict';

const Controller = require('egg').Controller;
const moment = require('moment');


class ArticleController extends Controller {
  // 获取文章列表
  async getArticleList() {
    const { ctx, app } = this;
    const { userId, type, page, keywords } = ctx.request.query;
    const res = await ctx.service.article.getArticleList(userId, type, page, keywords);
    ctx.body = res;
  }

  // 获取随机文章id
  async getRandomArticleId() {
    const { ctx, app } = this;
    const res = await ctx.service.article.getRandomArticleId();
    ctx.body = res;
  }

  // 发布新文章
  async releaseArticle() {
    const { ctx, app } = this;
    // 创建mysql事务
    let transaction = await app.model.transaction();
    const { userId, author, articleId, title, type, content, introduce, visible } = ctx.request.body;
    let res = {};
    try {
      // 写入文章详情数据
      const addContentRes = await ctx.service.file.writeArticle(articleId, author, content);
      if (addContentRes.success) {
        // 写入其他信息
        await app.model.ArticleList.create({
          id: articleId, user_id: userId, author, title, type, introduce, 
          hot: 0, visible, favour: 0, create_date: moment().format('YYYY-MM-DD HH:mm:ss'), update_date: moment().format('YYYY-MM-DD HH:mm:ss')
        }, {transaction})
        // 提交事务
        await transaction.commit();
        res = {
          success: true,
          articleId,
          msg: '操作成功'
        };
      } else throw Error(addContentRes.code)
    } catch (e) {
      // 回滚事务
      await transaction.rollback();
      res = {
        success: false,
        msg: '操作失败',
        code: e.toString(),
      };
    }
    ctx.body = res;
  }

  // 编辑更新文章
  async updateArticle() {
    const { ctx, app } = this;
    // 创建mysql事务
    const transaction = await app.model.transaction();
    const { articleId, author, title, type, content, introduce, visible } = ctx.request.body;

    let res = {};
    try {    
      // 更新文章详情数据
      const updateContentRes = await ctx.service.file.writeArticle(articleId, author, content)
      if (updateContentRes.success) {
        // 更新 其他信息
        await app.model.ArticleList.update({title, type, introduce, visible, update_date: moment().format('YYYY-MM-DD HH:mm:ss')}, { id: articleId }, {transaction})
        // 提交事务
        await transaction.commit();
        res = {
          success: true,
          msg: updateContentRes.msg,
          articleId,
        };
      } else {
        throw Error(updateContentRes.code)
      }
     
    } catch (e) {
      // 回滚事务
      await transaction.rollback();
      res = {
        success: false,
        msg: '操作失败',
        code: e.toString(),
      };
    }
    ctx.body = res;
  }

  // 删除文章
  async deleteArticle() {
    const { ctx, app } = this;
    // 创建mysql事务
    const transaction = await app.model.transaction()
    const { articleId, author } = ctx.request.body
    let res = {}
    try {
      // 删除详情信息
      const deleteRes = await ctx.service.file.deleteArticle(articleId, author)
      if (deleteRes.success) {
        // 删除其他信息
        await app.model.ArticleList.destroy({id: articleId}, {transaction})
        // 提交事务
        await transaction.commit();
        // 删除对应文章相关的图片静态资源
        await ctx.service.file.delete(articleId, 'directory');
        res = {
          success: true,
          msg: '操作成功',
          articleId,
        };
      } else throw Error(deleteRes.code)
   
    } catch (e) {
      // 回滚事务
      await transaction.rollback();
      res = {
        success: false,
        msg: '操作失败',
        code: e.toString(),
      };
    }
    ctx.body = res;

  }

  // 获取文章详情
  async getArticleDetail() {
    const { ctx, app } = this;
    const { id, author, update } = ctx.request.query;
    const res = await ctx.service.article.getArticleDetail(id, author, update);
    ctx.body = res;
  }

}

module.exports = ArticleController;
